﻿using System.Collections;
using WorkFlow.Engine.NSParser;

namespace WorkFlow.Engine.NSActivity.NSRoutes
{
    public class WfConditionParser
    {
        public bool ParseCondition(string cosExpress, bool condition)
        {
            return condition;
        }

        /// <summary>
        ///     根据表达式和对应的值计算逻辑表达式的值 例如(x>2&&y<3)||y>10
        /// </summary>
        /// <param name="consExpress">
        /// </param>
        /// <param name="values">x:1;y:3</param>
        public bool ParseCondition(string consExpress, string values)
        {
            #region 替换表达式里面的形参

            if (string.IsNullOrEmpty(values)) return false;
            string[] strs = values.Split(';');
            if (strs.Length == 0) return false;
            int count = strs.Length;

            var ht = new Hashtable();
            for (int i = 0; i < count; i++)
            {
                if (string.IsNullOrEmpty(strs[i])) continue;
                string[] temp = strs[i].Split(':');
                if (temp.Length == 2 && (!string.IsNullOrEmpty(temp[0])))
                {
                    if (!ht.ContainsKey(temp[0]))
                    {
                        ht[temp[0]] = temp[1];
                        consExpress = consExpress.Replace(temp[0], temp[1]);
                    }
                }
            }
            ht.Clear();

            #endregion 替换表达式里面的形参

            #region 对表达式求值

            return bool.Parse(WfExpressionEvaluator.Eval(consExpress).ToString().Trim());

            #endregion 对表达式求值
        }
    }
}